package com.jeasonzhao.commons.configuration;

import java.io.IOException;

import com.jeasonzhao.commons.utils.Algorithms;
import com.jeasonzhao.commons.utils.ResourceException;
import com.jeasonzhao.commons.utils.ResourceHelper;

public class ProductVersionInfo implements java.io.Serializable
{
    private static final long serialVersionUID = 1L;
    private static final String KEY_License = "License";
    private static final String KEY_ProductName = "ProductName";
    private static final String KEY_Vendor = "Vendor";
    private static final String KEY_HomePage = "HomePage";
    private static final String KEY_ProductPage = "ProductPage";
    private static final String KEY_EMail = "EMail";
    private static final String KEY_ProductOwner = "ProductOwner";
    private static final String KEY_CharacterSet = "CharacterSet";
    private String
        license = null
        ,productName = "<Unknown>"
        ,vendor = null
        ,homePage = null
        ,productPage = null
        ,email = null
        ,productowner = null
        ,charset = "chs"
        ,currentVersion = "<Unknown>"
        ,codeName = null
        ;
    public ProductVersionInfo(Class<?> cls,String strFileName)
    {
        initVersionInfo(cls,strFileName);
    }

    private void initVersionInfo(Class<?> cls,String strFileName)
    {
        if(Algorithms.isEmpty(strFileName))
        {
            strFileName="versionHistory.txt";
        }
        if(null == cls)
        {
            cls = ProductVersionInfo.class;
        }

        java.io.BufferedReader reader = null;
        currentVersion = "<Unknown>";
        productName = "<Unknown>";
        try
        {
            java.io.InputStream s=ResourceHelper.streamFromClassPath(cls,strFileName);
            if(null==s)
            {
                s=ResourceHelper.streamFromResource(cls,strFileName);
            }
            reader = new java.io.BufferedReader(new java.io.InputStreamReader(s));
            String strMaxVersion = "";
            String strMaxVersionFormated = "";
            String strCodeName = null;
            String strLine = null;
            while(null != (strLine = reader.readLine()))
            {
            	if(strLine.trim().length()<1||strLine.trim().charAt(0)=='#')
            	{
            		continue;
            	}
            	else if(strLine.trim().charAt(0)=='*')
                {
                    handleKeywords(strLine.trim());
                    continue;
                }
                String[] ars = strLine.trim().split("\\s");
                if(ars.length > 2)
                {
                    StringBuffer af = new StringBuffer();
                    String[] args = ars[1].split("\\.");
                    for(int n = 0;n < args.length;n++)
                    {
                        af.append(Algorithms.padLeft(args[n],6,'0'));
                    }
                    if(strMaxVersionFormated.compareTo(af.toString()) < 0)
                    {
                        strMaxVersion = ars[1];
                        strMaxVersionFormated = af.toString();
                        strCodeName = ars[0];
                    }
                }
            }
            if(strMaxVersion.length() > 0)
            {
                this.currentVersion = strMaxVersion;
            }
            if(strCodeName.length() > 0)
            {
                this.codeName = strCodeName;
            }
            s.close();
        }
        catch(IOException excep)
        {
        	excep.printStackTrace();
        }
        catch(ResourceException excep)
        {
        	excep.printStackTrace();
        }
        finally
        {
            if(null != reader)
            {
                try
                {
                    reader.close();
                }
                catch(IOException ex)
                {
                	ex.printStackTrace();
                }
            }
        }
    }

    private void handleKeywords(String str)
    {
        str = str.substring(1);
        int n = str.indexOf(" ");
        if(n <= 0)
        {
            return;
        }
        String key = str.substring(0,n).trim();
        String value = str.substring(n + 1).trim();
        if(value.length() < 1 || key.length() < 1)
        {
            return;
        }
        if(key.equalsIgnoreCase(KEY_License))
        {
            this.license = value;
        }
        else if(key.equalsIgnoreCase(KEY_ProductName))
        {
            this.productName = value;
        }
        else if(key.equalsIgnoreCase(KEY_Vendor))
        {
            this.vendor = value;
        }
        else if(key.equalsIgnoreCase(KEY_HomePage))
        {
            this.homePage = value;
        }
        else if(key.equalsIgnoreCase(KEY_ProductPage))
        {
            this.productPage = value;
        }
        else if(key.equalsIgnoreCase(KEY_EMail))
        {
            this.email = value;
        }
        else if(key.equalsIgnoreCase(KEY_ProductOwner))
        {
            this.productowner = value;
        }
        else if(key.equalsIgnoreCase(KEY_CharacterSet))
        {
            this.charset = value;
        }

    }

    public String getCodeName()
    {
        return this.codeName;
    }

    public String getCharset()
    {
        return this.charset;
    }

    public String getEmail()
    {
        return email;
    }

    public String getHomePage()
    {
        return homePage;
    }

    public String getLicense()
    {
        return license;
    }

    public String getProductName()
    {
        return productName;
    }

    public String getProductowner()
    {
        return productowner;
    }

    public String getProductPage()
    {
        return productPage;
    }

    public String getVendor()
    {
        return vendor;
    }

    public String getVersion()
    {
        return currentVersion;
    }

    public void setVersion(String version)
    {
        this.currentVersion = version;
    }

    public void setVendor(String vendor)
    {
        this.vendor = vendor;
    }

    public void setProductPage(String productPage)
    {
        this.productPage = productPage;
    }

    public void setProductowner(String productowner)
    {
        this.productowner = productowner;
    }

    public void setProductName(String productName)
    {
        this.productName = productName;
    }

    public void setLicense(String license)
    {
        this.license = license;
    }

    public void setHomePage(String homePage)
    {
        this.homePage = homePage;
    }

    public void setEmail(String email)
    {
        this.email = email;
    }

    public String getProductNameWithVersion()
    {
        return this.getProductName() + " V" + this.getVersion();
    }
}
